package com.jd.finance.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.finance.core.mapper.DictionaryMapper;
import com.jd.finance.core.pojo.entity.Dictionary;
import com.jd.finance.core.service.DictionaryService;
import com.jd.finance.core.service.OpreateLogService;
import com.jd.finance.result.BaseResult;
import com.jd.finance.util.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-20
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {


    @Resource
    private DictionaryMapper dictionaryMapper;

    @Autowired
    private AdminUtilsServiceImpl adminUtilsService;


    @Autowired
    private OpreateLogService opreateLogService;


    /**
     * 获取所有敏感词
     * @param uid
     * @return
     */
    @Override
    public BaseResult getAPPAllWord(Integer uid) {
        if (uid==null) {
            log.error("管理员id为空");
            return BaseResult.error().message("管理员id为空!");
        }

        if(!adminUtilsService.isInformation(uid)) {
            log.error("该用户非信息管理员身份!");
            return BaseResult.error().message("该用户非信息管理员身份!");
        }

         List<Dictionary> list=dictionaryMapper.selectList(null);

        if (list==null && list.size()==0) {
            log.error("未查询到敏感词信息!");
            return BaseResult.error().message("未查询到敏感词信息!");
        }

        return BaseResult.ok().data("dictionaryList",list);
    }

    /**
     * 添加敏感词
     * @param uid
     * @param dicText
     * @param request
     * @return
     */
    @Transactional
    @Override
    public BaseResult insertAPPWord(Integer uid, String dicText, HttpServletRequest request) {

        if (dicText=="" || uid==null) {
            log.error("敏感词信息为空！");
            return BaseResult.error().message("敏感词信息为空！");
        }


        if(!adminUtilsService.isInformation(uid)) {
            log.error("该用户非信息管理员身份!");
            return BaseResult.error().message("该用户非信息管理员身份!");
        }

        Dictionary dictionary=new Dictionary();
        dictionary.setDictText(dicText);
        int count=dictionaryMapper.insert(dictionary);
        if (count < 1) {
            log.error("添加失败");
            return BaseResult.ok().data("addminganci",0);
        }

        boolean add=opreateLogService.add(uid,"添加","添加敏感词", IpUtil.getIpAddr(request));
        if (!add) {
            log.error("添加操作日志失败!");
        }

        return BaseResult.ok().data("addminganci",1);
    }

    /**
     * 删除敏感词
     * @param uid
     * @param did
     * @param request
     * @return
     */
    @Transactional
    @Override
    public BaseResult deleteAPPWord(Integer uid, Integer did, HttpServletRequest request) {

        /**
         * 判断信息是否为空
         */
        if (uid==null || did==null) {
            log.error("删除敏感词信息为空");
            return BaseResult.error().message("删除敏感词信息为空!");
        }

        /**
         * 判断管理员身份是否为信息管理员
         */
        if(!adminUtilsService.isInformation(uid)) {
            log.error("该用户非信息管理员身份!");
            return BaseResult.error().message("该用户非信息管理员身份!");
        }


        QueryWrapper<Dictionary> dictionaryQueryWrapper=new QueryWrapper<>();
        dictionaryQueryWrapper.eq("did",did);
        Dictionary dictionary=dictionaryMapper.selectOne(dictionaryQueryWrapper);

        if (dictionary==null) {
            log.error("敏感词id不存在！");
            return BaseResult.error().message("敏感词id不存在！");
        }


        int count=dictionaryMapper.deleteById(did);
        if (count < 1) {
            log.error("删除失败！");
            return BaseResult.ok().data("shanchuminganci",0);
        }

        boolean add=opreateLogService.add(uid,"删除","删除敏感词", IpUtil.getIpAddr(request));
        if (!add) {
            log.error("添加操作日志失败!");
        }
        return BaseResult.ok().data("shanchuminganci",1);
    }


}
